package com.atguigu.controller;

import com.atguigu.common.config.exception.MyException;
import com.atguigu.model.system.SysOperLog;
import com.atguigu.model.system.SysRole;
import com.atguigu.result.Result;
import com.atguigu.service.SysRoleService;
import com.atguigu.service.SysUserRoleService;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysRole")
@Api(tags="角色管理")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @ApiOperation("查询角色所有数据")
    @GetMapping("getAll")
    public Result<List<SysRole>> getAll(){
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }
    @ApiOperation("分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<Page> getPage(@PathVariable Integer page, @PathVariable Integer limit, SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> objectPage = new Page<>(page, limit);
        String roleName = sysRoleQueryVo.getRoleName();
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)){
            lambdaQueryWrapper.like(SysRole::getRoleName,roleName);
        }
        Page<SysRole> page1 = sysRoleService.page(objectPage, lambdaQueryWrapper);
        return Result.ok(page1);
    }
    @ApiOperation("新增角色")
    @PostMapping("save")
    public Result save(@RequestBody SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        System.out.println(save);
        return Result.ok();
    }
    @ApiOperation("根据id获取角色")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable Integer id){
        SysRole sysRole = sysRoleService.getById(id);
        try {

        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20001,"自定义异常");
        }
        return Result.ok(sysRole);
    }
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result update(@RequestBody SysRole sysRole){
        boolean b = sysRoleService.updateById(sysRole);
        System.out.println(b);
        return Result.ok();
    }
    @ApiOperation("根据id删除角色")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        boolean b = sysRoleService.removeById(id);
        System.out.println(b);
        return Result.ok();
    }
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Integer> ids){
        boolean b = sysRoleService.removeByIds(ids);
        System.out.println(b);
        return Result.ok();
    }
    @ApiOperation("根据用户获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Integer userId){
        Map<String,Object> map=sysUserRoleService.toAssign(userId);
        return Result.ok(map);
    }
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        sysUserRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }


}
